package com.edu.compile.compiler;

import com.edu.compile.AbstractCompileResult;
import com.edu.compile.AbstractCompilerParser;
import com.edu.compile.config.HttpConfig;
import com.edu.compile.metadata.ParameterMetadata;
import com.edu.compile.template.ftl.block.Http;
import com.edu.model.dto.NodeMetadata;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;


@Component("http_compiler")
public class HttpCompiler extends AbstractCompilerParser<HttpConfig, Http> {

    @Override
    public void compile(AbstractCompileResult compileResult, NodeMetadata nodeMetadata, HttpConfig config, Http http) {

        List<ParameterMetadata> httpParams = config.getParameters();

        //必填项、默认值填充 BEGIN
        for (ParameterMetadata param : httpParams) {
            String paramName = param.getName();
            if (param.isRequired()) {
                http.addParamsCheck(paramName);
            } else {
                String defaultParam = param.getDefaultValue();
                if (StringUtils.isNotBlank(defaultParam)) {
                    http.putDefaultValueMap(paramName, defaultParam);
                }
            }
        }
    }
}
